import 'package:flutter/material.dart';
import '../../models/account_ye_model.dart';
import '../../l10n/app_localizations.dart';

typedef OnCellClick = void Function(String userID);
typedef IsShowIcon = void Function(bool isShow);
typedef OnClose = void Function();

class CzzhListView extends StatefulWidget {
  final OnCellClick onCellClick;
  final List<GroupListModel>? groupData;
  final IsShowIcon isShowIcon;
  final OnClose onClose; // 新增

  const CzzhListView({
    Key? key,
    required this.onCellClick,
    this.groupData,
    required this.isShowIcon,
    required this.onClose,

  }) : super(key: key);

  @override
  State<CzzhListView> createState() => _CzzhListViewState();
}

class _CzzhListViewState extends State<CzzhListView> {
  
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final height = 60 + (widget.groupData?.length ?? 0) * 70.0;

    return Center(
          child: Container(
            width: MediaQuery.of(context).size.width - 50,
            height: height,
            padding: const EdgeInsets.only(top: 10),
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(15),
            ),
            child: Column(
              children: [
                Stack(
                  children: [
                    Center(
                      child: Text(
                        AppLocalizations.of(context)!.selectRechargeAccount,
                        style: const TextStyle(
                          fontSize: 15,
                          color: Colors.black87,
                        ),
                      ),
                    ),
                    Positioned(
                      right: 15,
                      top: 0,
                      bottom: 0,
                      child: GestureDetector(
                        onTap: widget.onClose.call,
                        child: const SizedBox(
                          width: 16,
                          height: 16,
                          child: Image(
                            image: AssetImage('assets/images/差.png'), // 替代 UIImage(named: "差")
                          ),
                        ),
                      ),
                    )
                  ],
                ),
                const SizedBox(height: 10),
                Expanded(
                  child: ListView.separated(
                    padding: const EdgeInsets.symmetric(horizontal: 15),
                    itemCount: (widget.groupData?.length ?? 0),
                    separatorBuilder: (_, __) => const Divider(height: 1, color: Color(0xFFDCDFE6)),
                    itemBuilder: (context, index) {
                      final item = widget.groupData![index];
                      return Group2Cell(
                        model: item,
                        onTap: () {
                          widget.onCellClick(item.memberNumber ?? '');
                        },
                      );
                    },
                  ),
                ),
              ],
            ),
          ),
        );
  }
}

class Group2Cell extends StatelessWidget {
  final GroupListModel model;
  final VoidCallback onTap;

  const Group2Cell({
    Key? key,
    required this.model,
    required this.onTap,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final bool hasNickName = (model.memberNickName ?? '').isNotEmpty;

    return InkWell(
      onTap: onTap,
      child: SizedBox(
        height: 70,
        child: Row(
          children: [
            // 文本部分占据按钮左侧空间
            Expanded(
              child: Row(
                children: [
                  Flexible(
                    child: Text(
                      model.memberNumber ?? '',
                      style: const TextStyle(fontSize: 14, color: Colors.black),
                      overflow: TextOverflow.ellipsis,
                      maxLines: 1,
                    ),
                  ),
                  if (hasNickName) const SizedBox(width: 8),
                  if (hasNickName)
                    Flexible(
                      child: Text(
                        model.memberNickName ?? '',
                        style:
                            const TextStyle(fontSize: 14, color: Colors.black),
                        overflow: TextOverflow.ellipsis,
                        maxLines: 1,
                      ),
                    ),
                ],
              ),
            ),
            const SizedBox(width: 10), // 与按钮间距
            Container(
              height: 34,
              width: 80,
              alignment: Alignment.center,
              decoration: BoxDecoration(
                border: Border.all(color: Colors.blue),
                color: const Color(0xFFFAFAFF),
                borderRadius: BorderRadius.circular(17),
              ),
              child: Text(
                AppLocalizations.of(context)!.use,
                style: const TextStyle(
                  color: Colors.blue,
                  fontSize: 13,
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}
